package com.jbh.wanandroidkotlin.activity

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.google.android.material.tabs.TabLayout
import com.jbh.wanandroidkotlin.R
import com.jbh.wanandroidkotlin.fragment.MessageListFragment
import kotlinx.android.synthetic.main.activity_message.*
import kotlinx.android.synthetic.main.common_list_layout.*

const val TAB_1 = 0
const val TAB_2 = 1

class MessageActivity : BaseActivity() {

    /**
     * 当前Tab
     */
    private var currentPosition = 0

    private val title = listOf<String>("新消息","历史消息")

    private val listPage = mutableListOf<Fragment>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_message)
        listPage.add(MessageListFragment.newInstance(0))
        listPage.add(MessageListFragment.newInstance(1))
        messageViewPager.adapter = MessagePageAdapter(supportFragmentManager)
        initTab()
    }



    private fun initTab() {
        mTabLayout.apply {
            isTabIndicatorFullWidth = false
            setupWithViewPager(messageViewPager)
            addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    currentPosition =  tab!!.position
                    mSmartRefreshLayout.autoRefresh()
                }
                override fun onTabUnselected(tab: TabLayout.Tab?) {
                }
                override fun onTabReselected(tab: TabLayout.Tab?) {
                }
            })
        }
    }



    inner class MessagePageAdapter(val manager:FragmentManager) :FragmentPagerAdapter(manager){
        override fun getCount(): Int {
            return listPage.size
        }

        override fun getItem(position: Int): Fragment {
            return listPage[position]
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return title[position]
        }
    }
}